package edu.ccut.saturn.studio.dialog.viewerdialog;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher.SearchResult;
import edu.ccut.saturn.studio.core.search.dialog.OneCompontentSearchDialog;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnViewNode;

public class SaturnViewerDialog extends TitleAreaDialog {

	private Label idLabel;
	private Text idText;
	private Text nameText, valueText, descriptionText;

	private Combo typeCombo;

	private Label nameLabel, typeLabel, valueLabel, descriptionLabel, browser;

	private SaturnViewNode saturnViewerNode;


	public SaturnViewerDialog(Shell parentShell, SaturnViewNode saturnViewerNode) {
		super(parentShell);
		this.saturnViewerNode = saturnViewerNode;
	}

	@Override
	protected Control createDialogArea(Composite parent) {

		setMessage(SelectionCandidate.SETTING_ATTRIBUTE
				+ saturnViewerNode.getName().trim());
		setTitleImage(Activator.getImage(Images.SATURN_ICONS_MAXNAME));

		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		container.setLayout(gridLayout);
		container.setLayoutData(new GridData(GridData.FILL_BOTH));

		idLabel = new Label(container, SWT.NONE);
		final GridData gd_idLabel = new GridData();
		gd_idLabel.verticalIndent = 15;
		idLabel.setLayoutData(gd_idLabel);
		idLabel.setText("ID");

		idText = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		idText.setTextLimit(100);
		final GridData gd_idText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_idText.verticalIndent = 15;
		gd_idText.horizontalIndent = 10;
		idText.setLayoutData(gd_idText);
		idText.setText(saturnViewerNode.getId().trim());
		new Label(container, SWT.NONE);

		nameLabel = new Label(container, SWT.NONE);
		final GridData gd_nameLabel = new GridData();
		gd_nameLabel.verticalIndent = 15;
		nameLabel.setLayoutData(gd_nameLabel);
		nameLabel.setText(Messages.getString("SaturnViewerDialog.name")); //$NON-NLS-1$

		nameText = new Text(container, SWT.BORDER);
		nameText.setTextLimit(50);
		final GridData gd_textName = new GridData(SWT.FILL, SWT.CENTER, true,
				false, 2, 1);
		gd_textName.horizontalIndent = 10;
		gd_textName.verticalIndent = 15;
		nameText.setLayoutData(gd_textName);
		nameText.setText(saturnViewerNode.getName().trim());

		typeLabel = new Label(container, SWT.NONE);
		final GridData gd_typeLabel = new GridData();
		gd_typeLabel.verticalIndent = 15;
		typeLabel.setLayoutData(gd_typeLabel);
		typeLabel.setText(Messages.getString("SaturnViewerDialog.type")); //$NON-NLS-1$

		typeCombo = new Combo(container, SWT.BORDER | SWT.READ_ONLY);
		final GridData gd_textType = new GridData(SWT.FILL, SWT.CENTER, true,
				false, 2, 1);
		gd_textType.horizontalIndent = 10;
		gd_textType.verticalIndent = 15;
		typeCombo.setLayoutData(gd_textType);
		typeCombo.setItems(SelectionCandidate.viewTypes);
		typeCombo.select(0);
		typeCombo.setText(saturnViewerNode.getType().trim());

		valueLabel = new Label(container, SWT.NONE);
		final GridData gd_locationLabel = new GridData();
		gd_locationLabel.verticalIndent = 15;
		valueLabel.setLayoutData(gd_locationLabel);
		valueLabel.setText(Messages.getString("SaturnViewerDialog.value")); //$NON-NLS-1$

		valueText = new Text(container, SWT.BORDER);
		valueText.setTextLimit(100);
		final GridData gd_textLocation = new GridData(SWT.FILL, SWT.CENTER,
				true, false);
		gd_textLocation.horizontalIndent = 10;
		gd_textLocation.verticalIndent = 15;
		valueText.setLayoutData(gd_textLocation);
		valueText.setText(saturnViewerNode.getActionlocation().trim());

		browser = new Label(container, SWT.BORDER);
		browser.setImage(Activator.getImage(Images.AMPLIFY_PLUS));
		final GridData gd_imageLabel = new GridData(SWT.FILL, SWT.CENTER,
				false, false);
		gd_imageLabel.verticalIndent = 10;
		browser.setLayoutData(gd_imageLabel);

		browser.addMouseTrackListener(new MouseTrackAdapter() {
			public void mouseEnter(final MouseEvent e) {
				browser
						.setImage(Activator.getImage(Images.AMPLIFY_MINUS));
			}

			public void mouseExit(final MouseEvent e) {
				browser.setImage(Activator.getImage(Images.AMPLIFY_PLUS));
			}
		});
		browser.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
				OneCompontentSearchDialog dialog = new OneCompontentSearchDialog(getShell(),saturnViewerNode);
				
				if (dialog.open() == Window.OK) {
					SearchResult value = (SearchResult)dialog.getFirstResult();
					String filePath = value.getFile() + ""; //$NON-NLS-1$
					String tempPath = filePath.replace(value.getSrcName() + "", ""); //$NON-NLS-1$ //$NON-NLS-2$
					
					valueText.setText("/" + value.getProject().getName() + tempPath.replace("\\", "/")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
				
				}
				
//				ElementListSelectionDialog dialog1 = new ElementListSelectionDialog(
//						Display.getDefault().getShells()[0],
//						new SaturnSWTUtils.SaturnLabelDefaultProvider());
//				dialog1.setTitle(Messages.getString("SaturnViewerDialog.ChooseParaKeyValue")); //$NON-NLS-1$
//
//				if (!"jsp".equals(typeCombo.getText())) {
//					dialog1
//							.setElements(saturnViewerNode
//									.getParameterValueKeys());
//					if (dialog1.open() == Window.OK) {
//						valueText.setText((String) dialog1.getFirstResult());
//					}
//				} else {
//					jspPaths = saturnViewerNode.searchJSP();
//					dialog1.setElements(jspPaths);
//					if (dialog1.open() == Window.OK) {
//						valueText.setText((String) dialog1.getFirstResult());
//					}
//				}
			}
		});

		descriptionLabel = new Label(container, SWT.NONE);
		final GridData gd_descriptionLabel = new GridData();
		gd_descriptionLabel.verticalIndent = 15;
		descriptionLabel.setLayoutData(gd_descriptionLabel);
		descriptionLabel.setText(Messages.getString("SaturnViewerDialog.description")); //$NON-NLS-1$

		descriptionText = new Text(container, SWT.BORDER);
		descriptionText.setTextLimit(200);
		final GridData gd_textDec = new GridData(SWT.FILL, SWT.CENTER, true,
				false, 2, 1);
		gd_textDec.heightHint = 46;
		gd_textDec.horizontalIndent = 10;
		gd_textDec.verticalIndent = 15;
		descriptionText.setLayoutData(gd_textDec);
		descriptionText.setText(saturnViewerNode.getDescription().trim());

		nameText.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				if ("".equals(nameText.getText().trim())) { //$NON-NLS-1$
					setErrorMessage(Messages.getString("SaturnViewerDialog.Namelimit")); //$NON-NLS-1$
					getButton(IDialogConstants.OK_ID).setEnabled(false);
				} else {
					setErrorMessage(null);
					getButton(IDialogConstants.OK_ID).setEnabled(true);
				}
			}
		});

		return area;
	}

	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			saturnViewerNode.setDescription(descriptionText.getText().trim());
			saturnViewerNode.setActionlocation(valueText.getText().trim());
			saturnViewerNode.setType(typeCombo.getText().trim());
			saturnViewerNode.setName(nameText.getText().trim());
		}

		super.buttonPressed(buttonId);
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		super.createButtonsForButtonBar(parent);
		getButton(IDialogConstants.OK_ID).setEnabled(true);
	}

	@Override
	protected Point getInitialSize() {
		return new Point(546, 371);
	}

	protected int getShellStyle() {
		return SWT.CLOSE | SWT.RESIZE;
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME));
		newShell.setText(Messages.getString("SaturnViewerDialog.setAttrbute")); //$NON-NLS-1$
	}
}